/**
 * 

 */
package edu.vt.ece.rt.quorum.util;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import edu.vt.ece.rt.dstm.QObject;
import edu.vt.ece.rt.util.Logger.Logger;

/**
 * To Save the List of object read and write by Transaction
 * @author Sudhanshu
 *
 */
public class ReadWriteSet implements Serializable{
	public ConcurrentHashMap<String, QObject> readList;
	public ConcurrentHashMap<String, QObject> writeList;
	
	public ReadWriteSet(){
		readList = new ConcurrentHashMap<String, QObject>();
		writeList = new ConcurrentHashMap<String, QObject>();
	}
	
	public void addReadObject(QObject obj){
		//Check if object is already there in map, most probably in case of local access
		if(readList.get(obj.getId())!=null)
			readList.replace(obj.getId(), obj);
		else {
			readList.put(obj.getId(), obj);
		}
		
	}
	
	public void clearSet() {
		writeList.clear();
		readList.clear();
	}
	
	public void addWriteObject(QObject obj){
		//Check if object is already there in map, most probably in case of local access
		if(writeList.get(obj.getId()) != null) {
			writeList.replace(obj.getId(), obj);
			Logger.debug("Replacing write "+obj.getId());
		} else {
			writeList.put(obj.getId(), obj);
			Logger.debug("Putting write "+obj.getId());
		}	
	}
	
	public Set<QObject> getReadObjects(){
			return new HashSet(readList.values());
	}
	
	public Set<QObject> getWriteObjects(){
		return new HashSet(writeList.values());
	}

	/**
	 * Clears all the entries in set
	 */
	public void Clear() {
		readList = new ConcurrentHashMap<String, QObject>();
		writeList = new ConcurrentHashMap<String, QObject>();	
	}
}
